/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Bean.LogInBean;
import DAL.AdminAccount;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Jessie
 */
public class LoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uname = request.getParameter("txtUsername");
        String password = request.getParameter("txtPassword");
        // <editor-fold defaultstate="collapsed" desc="khai bean, tác giả Dương Nam Phương, sửa lại theo MVC">
        LogInBean bean = new LogInBean();
        AdminAccount admin = bean.LogIn(uname, password);
        String url = "loginPage.jsp";
        if (admin != null) {
            url = "admin.jsp";
            HttpSession session = request.getSession();
            session.setAttribute("ADMIN", admin);
        } else {
            request.setAttribute("INVALID", "Invalid username and password! Please try again");
        }
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="kiểm tra login không đúng mô hình MVC (BỎ)">
        /*try {
         String uname = request.getParameter("txtUsername");
         String password = request.getParameter("txtPassword");
         Connection conn = DAO.makeConnection();
         PreparedStatement prepare = conn.prepareStatement("Select * from AdminAccount where uname=? and upass=?");
         prepare.setString(1, uname);
         prepare.setString(2, password);
         prepare.execute();
         ResultSet rs = prepare.executeQuery();
         String url = "loginPage.jsp";
         if (rs.next()) {
         AdminAccount admin = new AdminAccount();
         admin.setUsername(rs.getString("uname"));
         admin.setPassword(rs.getString("upass"));
         admin.setFullName(rs.getString("name"));
         HttpSession session = request.getSession();
         session.setAttribute("ADMIN", admin);
         url = "admin.jsp";
         }
         request.setAttribute("INVALID", "Invalid username and password! Please try again");
         RequestDispatcher rd = request.getRequestDispatcher(url);
         rd.forward(request, response);

         } catch (SQLException ex) {
         Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
         } finally {
         out.close();
         }*/
        // </editor-fold>
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
